from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from DButils import Mysql
import time



class Registerchecker:
    def __init__(self, username, password, repassword):
        self.username = username
        self.password = password
        self.repassword = repassword

    def test_register(self):
        username = self.username
        password = self.password
        repassword = self.repassword
        try:
            # 创建一个谷歌浏览器对象
            register = webdriver.Chrome()

            # 打开度小满理财系统
            register.get("http://localhost:90")

            # 窗口最大化
            register.maximize_window()
            time.sleep(2)

            # 等待显示系统登录首页
            WebDriverWait(register, 4, 0.5).until(EC.presence_of_element_located((By.CLASS_NAME, 'lear-login')))
            # 点击注册
            register.find_element(By.XPATH, '/html/body/div/div/div/div[2]/a').click()

            # 等待显示注册界面相关内容
            WebDriverWait(register, 5, 0.5).until(EC.presence_of_element_located((By.XPATH, '//*[@id="username"]')))

            # 输入用户名，密码，确认密码
            register.find_element(By.XPATH, '//*[@id="username"]').send_keys(f'{username}')
            register.find_element(By.XPATH, '//*[@id="password"]').send_keys(f'{password}')
            register.find_element(By.XPATH, '//*[@id="repassword"]').send_keys(f'{repassword}')
            time.sleep(1)

            # 点击注册
            register.find_element(By.XPATH, '//*[@id="login_btn"]').click()
            time.sleep(5)

            # 退出浏览器
            register.quit()

        except Exception as e:
            print("注册失败！", e)

    def test_verify_register(self):
        username = self.username
        # 验证数据库中是否存在注册的数据
        db1 = Mysql("127.0.0.1", 3306, "root", "root", "finance", "utf8")
        sql_s1 = f"select * from user where username = '{username}'"
        result = db1.select(sql_s1)
    #     sql = "select * from user where username = %s"
    #     param = ["lisi"]
    #     data = MysqlDevelop.select(sql, param)
        if len(result) == 1:
            print("注册成功！")
        else:
            print("注册失败！")
if __name__ == '__main__':
    test_register = Registerchecker("小红", "123456", "123456")
    test_register.test_register()
    test_register.test_verify_register()


